{% extends "repo/repo.html" %}

{% block css %}
{% endblock %}

{% block repo_description %}
<div class="description bubble">
  <p>{{repo.desc}}<a href="#README.md" class="readmore"><i class="icon-double-angle-right"></i><span>阅读更多</span></a></p>
</div>
{% endblock %}

{% block subcontainer %}
<div class="files bubble" id="resize-container">
  <div class="table-container" id="source">
  {% if repo.status != 0 %}
    <div id="id-repos-loading" class="ajaxLoader"><p>正在初始化仓库...</p><img src="/static/img/loading.gif"></div>
  {% else %}
    <div class="theme-selector">
        {% include "repo/branch_nav.html" %}
        <span class="current-branch-name">
          <a href="/{{user_name}}/{{repo_name}}/{%if current == 'index' or current == 'tree' or current == 'blob' %}tree{%endif%}{%if current == 'commits' %}commits{%endif%}{%if current == 'refs_graph' %}refs/graph{%endif%}/{{refs}}/">{{repo_name}}</a><span class="divider">/</span>
          <span id="repo-path"></span>
        </span>
    </div>
    <table class="table">
      <tbody>
        {% if not tree %}
          <tr>
            <td>
              <p class="alert alert-error"><img src="/static/img/icons/alert-triangle-yellow.png" alt="">没有源代码、二进制文件，或者没有查看源代码权限，半公开和私有项目需要申请成为成员才能查看源代码。</p>
            </td>
          </tr>
        {% elif tree.tree %}
          {% for file_attrs in tree.tree %}
            <tr>
              <td class="file-name">
                {% if file_attrs.type == 'tree' %}
                  <span class="icon"><img src="/static/img/file_dir.png" alt="dir"></span>
                {% else %}
                  <span class="icon"><img src="/static/img/file_txt.png" alt="file"></span>
                {% endif %}
                <a href="/{{user_name}}/{{repo_name}}/{%if file_attrs.type == 'tree'%}tree{%else%}blob{%endif%}/{{refs}}/{%if path != '.' %}{{path}}{%endif%}{{file_attrs.relative_path}}" title="{{file_attrs.relative_path}}">{{file_attrs.relative_path}}</a>
              </td>
              <td class="commit-info">
                {% if file_attrs.last_commit_message|length > 80 %}
                  <a href="javascript:void(0)" data-toggle="popover" data-content="{{file_attrs.last_commit_message}}" data-placement="bottom" class="commit">{{file_attrs.last_commit_message|truncatechars:80}}</a>
                {% else %}
                  <a href="javascript:void(0)" class="commit">{{file_attrs.last_commit_message}}</a>
                {% endif %}

                {% if file_attrs.real_author_name == '' %}
                  <span class="commiter">@{{file_attrs.author_name}}</span>
                {% else %}
                  <a href="/{{file_attrs.real_author_name}}/" class="commiter">@{{file_attrs.author_name}}</a>
                {% endif %}
              </td>
              <td class="date">
                <span class="date unixtime">{{file_attrs.author_time}}</span>
              </td>
            </tr>
          {% endfor %}
        {% else %}
          <tr>
            <td>
              {% if current == 'index' %}
                <div class="git-empty">
                <fieldset>
                <legend>设置 user.name 和 user.email</legend>
                <pre class="dark">git config --global user.name "{{user.username}}"
git config --global user.email "{{user.email}}"
                </pre>
                </fieldset>
                <fieldset>
                <legend>创建一个全新的仓库</legend>
                <pre class="dark">git init {{repo_name}}
cd {{repo_name}}
touch README
git add README
git commit -m 'first commit'
<span class="clone">git remote add origin https://gitshell.com/{{user_name}}/{{repo_name}}.git</span>
git push -u origin master
                </pre>
                </fieldset>
                <fieldset>
                <legend>导入现有的仓库</legend>
                <pre class="dark">cd existing_git_repo
<span class="clone">git remote add origin https://gitshell.com/{{user_name}}/{{repo_name}}.git</span>
git push -u origin master
                </pre>
                </fieldset>
                </div>
              {% else %}
              <p class="alert blank-alert"><i class="icon-bell"></i>没有提交源代码</p>
              {% endif %}
            </td>
          </tr>
        {% endif %}
      </tbody>
    </table>
  {% endif %}

  </div>
</div>

{% if repo.status == 0 and readme_md %}
  <div id="README.md" class="readme bubble">
    <div class="title">
      <h2 class="heading">README.md</h2>
    </div>
    <div id="id_readme_md" class="markdown hide">{{readme_md}}</div>
  </div>
{% endif %}

{% endblock %}

{% block subjs %}

{% if readme_md %}
<script src="/static/js/app/markdown/marked.js"></script>
<script src="/static/js/app/caja/html-css-sanitizer-minified.js"></script>
{% endif %}
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="/static/js/jquery-ui-1.10.3.custom.min.js"></script>
<script src="/static/js/treeview.js"></script>
<script>
  $(function(){

    {% comment %}
    var descriptionHeight = $('.description p').height();

    $('.description p').css({
      height: '20px',
      overflow: 'hidden'
    });

    $('.description .readmore').css({
      position: 'relative',
      top: '-10px'
    }).click(
      function(e){ 
        e.preventDefault();
        if ($('.description p').height() < descriptionHeight) {
          $('.description p').animate({
            height: descriptionHeight
          }, 'fast');
          $(this).find('span').text('收起来');
        } else {
          $('.description p').animate({
            height: '20px'
          }, 'fast');
          $(this).find('span').text('阅读更多');
        }
      }
    );

    // File browser resizable & treeview
    $("#file-browser").resizable({
      handles: "e",
      grid: 10,
      minWidth: 200,
      maxWidth: 300
    });

    $("#file-browser > .nav").treeview({
      collapsed: true,
      //unique: true,
      animated: "fast"
    });

    $("#file-browser").bind("resize",
      function(event, ui){
        var fileBrowserWidth = $("#file-browser").width();
        $("#source").css("margin-left", fileBrowserWidth);
      }
    );
    {% endcomment %}

    // Color Scheme
    var switcher = $("#switcher"),
        themes = $("#themes");
    switcher.click(
        function(e){
          e.preventDefault();
          themes.slideToggle(100);
        }
    );

    // Bootstrap/popover
    $('[data-toggle="popover"]').hover(
        function(){
          $(this).popover('show');
        }, function(){
          $(this).popover('hide');
        }
    );

  });
</script>
<script>
  $(function(){
    set_path_href = function() {
      var base_href_path = '/{{user_name}}/{{repo_name}}/tree/{{refs}}/';
      var path = '{{path}}';
      if(path == '.') {
        return;
      }
      var paths = path.split('/');
      path_href_html = '';
      for(x in paths) {
        subpath = paths[x];
        if(subpath == '') {
          break;
        }
        base_href_path = (base_href_path + subpath + '/');
        if(path.substr(-1) === "/") {
          if(x != (paths.length-2)) {
            path_href_html = (path_href_html + ' <a href="' + base_href_path + '">' + subpath + '</a> /');
          } else {
            path_href_html = (path_href_html + ' ' + subpath + ' /');
          }
        } else {
          if(x != (paths.length-1)) {
            path_href_html = (path_href_html + ' <a href="' + base_href_path + '">' + subpath + '</a> /');
          } else {
            path_href_html = (path_href_html + ' ' + subpath);
          }
        }
      }
      $('#repo-path').html(path_href_html);
    };
    set_path_href();

    {% if readme_md %}
    marked.setOptions({ gfm: true, tables: true, breaks: false, pedantic: false, sanitize: false, smartLists: true, smartypants: false, langPrefix: 'lang-' });
    var html = $('#id_readme_md').html();
    html = $('<div/>').html(html).text();
    var sanitized_html = html_sanitize(marked(html), function(url) { return url; }, function(id) { return id; })
    $('#id_readme_md').html(sanitized_html);
    $('#id_readme_md').show();
    {% endif %}
    {% if repo.status != 0 %}
    function checkout_status_reload() {
      $.post('/ajax/repo/find/', {csrfmiddlewaretoken: '{{ csrf_token }}', 'id': {{repo.id}}}, function(json){
        if(json.status == 0) {
          window.location.reload(true);
        }
      });
    }
    setTimeout(checkout_status_reload, 1000);
    setTimeout(checkout_status_reload, 4000);
    setTimeout(checkout_status_reload, 9000);
    setInterval(checkout_status_reload, 10000);
    {% endif %}
    $('[data-toggle="popover"]').popover({ animation: false });
  });
</script>

{% endblock %}
